<form class="layui-form seller-form"  action="" >
    <div class="layui-form-item">
        <div class="layui-inline">
            <label class="layui-form-label seller-inline-2">广告名称：</label>
            <div class="layui-input-inline seller-inline-4">
                <input type="text" name="name" lay-verify="title" placeholder="请输入广告名称" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-inline">
            <label class="layui-form-label seller-inline-2">更新时间：</label>
            <div class="layui-input-inline seller-inline-4">
                <input type="text" id="utime" name="utime" lay-verify="title" placeholder="开始时间 到 结束时间" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-inline">
            <button class="layui-btn layui-btn-sm" lay-submit lay-filter="search"><i class="iconfont icon-chaxun"></i>筛选</button>

            <button type="button" class="layui-btn layui-btn-sm add"><i class="layui-icon">&#xe608;</i> 添加</button>
        </div>
    </div>
</form>

<div class="table-body">
	<table id="carousel" lay-filter="carousel"></table>
</div>

<script>
    layui.use(['table','form','layer','laydate'],function(){
        var layer = layui.layer, table = layui.table,form = layui.form,date = layui.laydate;
        //执行渲染
        table.render({
            elem: '#carousel', //指定原始表格元素选择器（推荐id选择器）
            height: 'full-99',
            cellMinWidth: '80',
            page: 'true',
            limit:'20',
            id:'carousel',
            url: "{:url('Carousel/index')}",
            cols: [[ //标题栏
                {field:'id', width:90, title:'ID', align: 'center'},
                {field: 'name', title: '广告名称',align:'center'},
                {field: 'img', title: '广告图片',align:'center',width:100, templet: function(data){
                    return '<a href="javascript:void(0);" onclick=viewImage("'+data.img+'")><image style="max-width:30px;max-height:30px;" src="'+data.img+'"/></a>';
                }},
                {field: 'carousel_type', title: '广告类型',align:'center',width:100},
                {field: 'value', title: '广告地址',align:'center'},
                {field: 'pname', title: '所属广告位',align:'center'},
                {field: 'ctime',sort: true, title: '创建时间' ,align:'center',width:170},
                {field: 'utime',sort: true, title: '更新时间' ,align:'center',width:170},
                {field: 'sort', sort: true, title: '排序',align:'center',width:80},
                {width:140, title:'操作',align:'center', toolbar:'#carouselBar'}
            ]] //设置表头
            //,…… //更多参数参考右侧目录：基本参数选项
        });

        date.render({
            elem:'#utime',
            range: '到',
						trigger: 'click' //自动弹出控件的事件，采用click弹出
        });

        form.on('submit(search)', function(data){
            layui.table.reload('carousel', {
                where: data.field
                ,page: {
                    curr: 1 //重新从第 1 页开始
                }
            });
            return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
        });

        //添加
        $(document).on('click','.add',function(){
            // window.location.href = "{:url('Carousel/add')}";
            JsGet("{:url('Carousel/add')}", function (e) {
                    // if (e.status) {
                        window.box = layer.open({
                            type: 1,
                            content: e,
                            area: setpage(600, 500),
                            title: '添加广告',
                            btn: ['保存', '关闭'],
                            yes: function () {
                                var formData = form.val("add_carouset");
                                JsPost("{:url('Carousel/add')}", formData, function (res) {
                                    if (res.status) {
                                        layer.msg(res.msg, { time: 1300 }, function () {
                                            layer.closeAll();
                                            table.reload('carousel');
                                        });
                                    } else {
                                        layer.msg(res.msg);
                                    }
                                })
                            }
                        })
                    // } else {
                    //     layer.msg(e.msg);
                    // }
                })
        });

        form.on('submit(edit)',function(data){
            JsPost("{:url('Carousel/edit')}",{data:data.field},function(res){
                if(res.status){
                    layer.close(window.box);
                    layer.msg(res.msg, {time:1300},function(){
                        table.reload('carousel');
                    });
                }else{
                    layer.msg(res.msg);
                }
            });
        });

        //监听工具条
        table.on('tool(carousel)', function(obj){ //注：tool是工具条事件名，test是table原始容器的属性 lay-filter="对应的值"
            var data = obj.data; //获得当前行数据
            var layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
            if(layEvent === 'del'){ //删除
                layer.confirm('真的要删除么',{icon: 3}, function(index){
                    JsGet("{:url('Carousel/del')}?id=" + data.id, function(res){
                        if(res.status){
                            obj.del(); //删除对应行（tr）的DOM结构，并更新缓存
                            layer.close(index);//向服务端发送删除指令
                        }
                        layer.msg(res.msg);
                    })
                });
            } else if(layEvent === 'edit'){ //编辑
                // window.location.href = "{:url('Carousel/edit')}?id="+data.id;
                JsGet("{:url('Carousel/edit')}?id=" + data.id, function (e) {
                    // if (e.status) {
                        window.box = layer.open({
                            type: 1,
                            content: e,
                            area: setpage(600, 500),
                            title: '编辑广告',
                            btn: ['保存', '关闭'],
                            yes: function (index) {
                                var formData = form.val("edit_carouset");
                                JsPost("{:url('Carousel/edit')}", formData, function (res) {
                                    if (res.status) {
                                        layer.msg(res.msg, { time: 1300 }, function () {
                                            layer.close(index);
                                            table.reload('carousel');
                                        });
                                    } else {
                                        layer.msg(res.msg);
                                    }
                                })
                            }
                        })
                    // } else {
                    //     layer.msg(e.msg);
                    // }
                })
            }
        });

    })
</script>

<script type="text/html" id="carouselBar">
    <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>
